<?php

namespace app\admin\controller\kuyou;

use app\common\controller\Backend;
use think\Db;
/**
 * 客户对账单
 *
 * @icon fa fa-circle-o
 */
class CustomersRecon extends Backend
{

    /**
     * AccountsReceivable模型对象
     * @var \app\admin\model\kuyou\AccountsReceivable
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set',$setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $this->model = new \app\admin\model\kuyou\AccountsReceivable;
        $this->view->assign("businessTypeList", $this->model->getBusinessTypeList());

        $this->view->assign("customers", $this->model->getCustomersList());
        $this->view->assign("suppliers", $this->model->getSuppliersList());
        $this->view->assign("saleList", $this->model->getSaleList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $data = input();
            $where = [];
            $map = [];
            if(isset($data['filter'])){
                $filter = (array)json_decode($data['filter'], true);
                $filter = $filter ? $filter : [];
                if(!empty($filter['purchase_time'])){
                    $str = explode(' - ',$filter['purchase_time']);
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
                if(!empty($filter['customers_id'])){
                    $map['sp.id'] = trim($filter['customers_id']);
                }
                if(!empty($filter['staff_member_id'])){
                    $where['staff_member_id'] = trim($filter['staff_member_id']);
                }
                if(!empty($filter['id'])){
                    $map['sp.id'] = ['<',$filter['id']];
                }
            }
            $where = (new Goods())->setWhere($where);
            $map = (new Goods())->setWhere($map,'sp');

            $sup = Db::name('kuyou_customers')->alias('sp')
                ->join('kuyou_customertype gt','sp.customertype_id=gt.id')
                ->where($map)
                ->field('sp.id,sp.opening_prepayment,sp.customer_code,sp.customer_name,sp.receive_account,gt.type_name')
                ->order('sp.customer_code asc')
                ->select();

            $field = empty(input('field'))?'createtime':trim(input('field'));
            $order = empty(input('sort'))?'ASC':trim(input('sort'));

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $price_decimal  = $setList['price_decimal']['value'];
            $stmen = Db::name('kuyou_staff_member')->where((new Goods())->setWhere())->column('id','name');


            $listarr = [];
            $all_sale_money = 0;
            $all_receivable = 0;
            $all_cost = 0;
            $all_real_money = 0;
            $all_balance = 0;
            foreach ($sup as $sup_k=>$sup_v){
                $sale_money = 0;
                $receivable = 0;
                $cost = 0;
                $real_money = 0;
                $balance = 0;
                $sup_v['receive_account'] = round($sup_v['receive_account']-$sup_v['opening_prepayment'],$price_decimal);
                $listarr[] = [
                    'customer_name' => $sup_v['customer_name'],
                    'purchase_time' => '',
                    'purchase_code' => '期初余额',
                    'business_type' => '',
                    'sale_money'    => '',
                    'receivable'    => '',
                    'amount'        => '',
                    'cost'          => '',
                    'real_money'    => '',
                    'balance'       => $sup_v['receive_account'],
                    'staff_member'  => '',
                    'remark'        => ''
                ];
                $balance += $sup_v['receive_account'];

                $list = Db::name('kuyou_accounts_receivable')
                    ->where($where)
                    ->where(['customers_id'=>$sup_v['id'],'business_type'=>['not in',[1,2]]])
                    ->order([$field=>$order])
                    ->select();
                foreach ($list as $list_k=>$list_v){
                    if($list_v['business_type']==7){
                        $balance     -= $list_v['receivable_money'];
                        $balance     += $list_v['real_money'];
                        $sale_money  -= $list_v['sale_money'];
                        $receivable  -= $list_v['receivable_money'];
                        $cost        -= $list_v['cost'];
                        $real_money  -= $list_v['real_money'];
                        $listarr[] = [
                            'customer_name' => $sup_v['customer_name'],
                            'purchase_time' => !empty($list_v['purchase_time'])?date('Y-m-d',$list_v['purchase_time']):'',
                            'purchase_code' => $list_v['purchase_code'],
                            'business_type' => $list_v['business_type'],
                            'sale_money'    => -$list_v['sale_money'],
                            'receivable'    => -$list_v['receivable_money'],
                            'amount'        => -$list_v['amount'],
                            'cost'          => -$list_v['cost'],
                            'real_money'    => -$list_v['real_money'],
                            'balance'       => round($balance,$price_decimal),
                            'staff_member'  => isset($stmen[$list_v['staff_member_id']])?$stmen[$list_v['staff_member_id']]:'',
                            'remark'        => $list_v['remark'],
                        ];
                    }else{
                        $balance     += $list_v['receivable_money'];
                        $balance     -= $list_v['real_money'];
                        $sale_money  += $list_v['sale_money'];
                        $receivable  += $list_v['receivable_money'];
                        $cost        += $list_v['cost'];
                        $real_money  += $list_v['real_money'];
                        $listarr[] = [
                            'customer_name' => $sup_v['customer_name'],
                            'purchase_time' => !empty($list_v['purchase_time'])?date('Y-m-d',$list_v['purchase_time']):'',
                            'purchase_code' => $list_v['purchase_code'],
                            'business_type' => $list_v['business_type'],
                            'sale_money'    => $list_v['sale_money'],
                            'receivable'    => $list_v['receivable_money'],
                            'amount'        => $list_v['amount'],
                            'cost'          => $list_v['cost'],
                            'real_money'    => $list_v['real_money'],
                            'balance'       => round($balance,$price_decimal),
                            'staff_member'  => isset($stmen[$list_v['staff_member_id']])?$stmen[$list_v['staff_member_id']]:'',
                            'remark'        => $list_v['remark'],
                        ];
                    }
                }
                $listarr[] = [
                    'customer_name' => $sup_v['customer_name'],
                    'purchase_time' => '',
                    'purchase_code' => '小计',
                    'business_type' => '',

                    'sale_money'    => round($sale_money,$price_decimal),
                    'receivable'    => round($receivable,$price_decimal),
                    'amount'        => '',
                    'cost'          => round($cost,$price_decimal),
                    'real_money'    => round($real_money,$price_decimal),
                    'balance'       => round($balance,$price_decimal),
                    'staff_member'  => '',
                    'remark'        => ''
                ];
                $all_sale_money += $sale_money;
                $all_receivable += $receivable;
                $all_cost       += $cost;
                $all_real_money += $real_money;
                $all_balance    += $balance;
            }

            $result = array("total" => sizeof($listarr),
                "rows" => $listarr,
                "all_sale_money" => $all_sale_money,
                "all_receivable" => $all_receivable,
                "all_cost" => $all_cost,
                "all_real_money" => $all_real_money,
                "all_balance" => $all_balance,
            );

            return json($result);
        }
        return $this->view->fetch();
    }

}
